<!DOCTYPE html>
<html lang="en"><head>
    <title>CZH-DEV BLOG</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="format-detection" content="telephone=no" />
    <meta name="theme-color" content="#000084" />
    <link rel="icon" href="https://czh-dev.gitee.io/czh-blog.gitee.io//favicon.ico">
    <link rel="canonical" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">
    
    
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"></button>
            <a class="brand" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">CZH-DEV BLOG</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/about/">
                                    
                                    <span>About</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/post/">
                                    
                                    <span>All posts</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/ebook/">
                                    
                                    <span>Resource</span>
                                </a>
                            </li>
                        
                    
                </ul>
            </div>
        </div>
    </div>
</nav><div id="content" class="container">
<div style="display: flex;">
  <div class="row-fluid navmargin">
    <div class="page-header">
      <h1>Redis基本操作 - Fri, Feb 10, 2023</h1>
    </div>
    <p class="lead"></p>
    <h1 id="redis缓存基础操作">Redis缓存基础操作</h1>
<blockquote>
<p>一般什么数据放在Redis中：</p>
<ul>
<li>不经常改变的数据</li>
<li>经常改变，但是可靠性要求不高的数据</li>
<li>首页信息，通常改变少，可以放入到Redis中</li>
</ul>
</blockquote>
<h2 id="string类型">string类型</h2>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110123192.png" alt="image-20220302165143437"></p>
<ol>
<li>查看所有的key（不会按顺序排序）&ndash; <strong>set</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>keys <span style="color:#f92672">*</span> <span style="color:#75715e">-- 查看所有的key	
</span></span></span></code></pre></div><ol start="2">
<li>
<p>添加一条数据（key，value）&ndash; <strong>set, setnx</strong></p>
<p><a href="">set：</a></p>
<ul>
<li>如果key数据不存在，会添加一条数据</li>
<li>否则，<strong>会覆盖</strong>上一条数据</li>
</ul>
<p><a href="">setnx：</a></p>
<ul>
<li>如果key数据不存在，会添加一条数据</li>
<li>否则不会添加数据（<strong>不会覆盖</strong>）</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#66d9ef">set</span> <span style="color:#66d9ef">key</span> value <span style="color:#75715e">-- 输入set自动提示（若存在会覆盖）
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>setnx <span style="color:#66d9ef">key</span> value <span style="color:#75715e">-- 不会覆盖数据
</span></span></span></code></pre></div><ol start="3">
<li>根据key查询value &ndash; <strong>get</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#66d9ef">get</span> <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 输入get自动提示
</span></span></span></code></pre></div><ol start="4">
<li>
<p>对数字进行加法(偏移) &ndash; <strong>incr, incrby</strong></p>
<p><strong>偏移词：</strong></p>
<ul>
<li>incr：加一</li>
<li>incrby：加多个</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>incr <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 将数字+1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>incrby <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">increment</span> <span style="color:#75715e">-- 给数字key添加increment
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">get</span> age <span style="color:#75715e">-- &#34;18&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>incrby age <span style="color:#ae81ff">20</span> <span style="color:#75715e">-- &#34;38&#34;
</span></span></span></code></pre></div><ol start="5">
<li>
<p>对数字进行减法(偏移) &ndash; <strong>decr, decrby</strong></p>
<p><strong>偏移词：</strong></p>
<ul>
<li>decr：减一</li>
<li>decrby：减多个</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>decr <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 将数字-1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>decr <span style="color:#66d9ef">key</span> decrement <span style="color:#75715e">--将数字key减少decrement
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">get</span> age <span style="color:#75715e">-- &#34;38&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>decrby age <span style="color:#ae81ff">20</span> <span style="color:#75715e">-- &#34;18&#34;
</span></span></span></code></pre></div><ol start="6">
<li>删除数据 &ndash; <strong>del</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>del <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span>...] <span style="color:#75715e">-- 删除，没有该key返回0
</span></span></span></code></pre></div><ol start="7">
<li>创建并且在某段时间后删除 &ndash; <strong>setex</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>setex <span style="color:#66d9ef">key</span> seconds value 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>setex email <span style="color:#ae81ff">10</span> rose <span style="color:#75715e">-- 生成一个key为email，value为rose的数据，并在十秒后自动删除
</span></span></span></code></pre></div><ol start="8">
<li>查看自动删除时间（配合7.使用）&ndash; <strong>ttl</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>ttl <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 查看key被自动删除还剩多少时间，已经被删除会返回-2
</span></span></span></code></pre></div><ol start="9">
<li>批量添加数据 &ndash; <strong>mset</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>mset <span style="color:#66d9ef">key</span> value [<span style="color:#66d9ef">key</span> value...]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>mset k1 v1 k2 v2 k3 v3 k4 v4 <span style="color:#75715e">-- 批量生成，中间只需要空格
</span></span></span></code></pre></div><ol start="10">
<li>追加数据 &ndash; <strong>append</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>append <span style="color:#66d9ef">key</span> value <span style="color:#75715e">-- 往一个key后追加数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">set</span> name czh <span style="color:#75715e">-- &#34;name&#34;——&#34;czh&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>append name jack <span style="color:#75715e">-- &#34;name&#34;——&#34;czhjack&#34;
</span></span></span></code></pre></div><ol start="11">
<li>批量查询数据 &ndash; <strong>mget</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>mget <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span>...] <span style="color:#75715e">-- 批量查询key的数据（中间使用空格分隔即可）
</span></span></span></code></pre></div><ol start="12">
<li>替换 &ndash; <strong>setrange</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>setrange <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">offset</span> value <span style="color:#75715e">-- 将key中offset位置的换成value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">get</span> name <span style="color:#75715e">-- &#34;czhjack&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>setrange name <span style="color:#ae81ff">2</span> w <span style="color:#75715e">-- &#34;czwjack&#34;
</span></span></span></code></pre></div><h2 id="hash类型">hash类型</h2>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110123819.png" alt="image-20220302165211947"></p>
<ol>
<li>
<p>添加数据 &ndash; <strong>hset， hsetnx， hmset</strong></p>
<p><a href="">hset：</a></p>
<ul>
<li>如果key中的hkey数据不存在，会添加一条数据</li>
<li>否则，<strong>会覆盖</strong>上一条数据</li>
</ul>
<p><a href="">hsetnx：</a></p>
<ul>
<li>如果key中hkey数据不存在，会添加一条数据</li>
<li>否则不会添加数据（<strong>不会覆盖</strong>）</li>
</ul>
<p><a href="">hmset：</a></p>
<ul>
<li>批量添加同一个key中的数据</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hset <span style="color:#66d9ef">key</span> field value <span style="color:#75715e">-- 向key中添加字段和value（可以看成有两个key）
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>hsetnx <span style="color:#66d9ef">key</span> field value <span style="color:#75715e">-- 不会覆盖数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>hmset <span style="color:#66d9ef">key</span> field value [field value ...] <span style="color:#75715e">-- 批量添加同一个key中的数据
</span></span></span></code></pre></div><ol start="2">
<li>查询数据 &ndash; <strong>hget， hmget</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hget <span style="color:#66d9ef">key</span> field <span style="color:#75715e">-- 查询key中的field对应的value值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>hmget <span style="color:#66d9ef">key</span> fiels [field ...] <span style="color:#75715e">-- 批量查询数据
</span></span></span></code></pre></div><ol start="3">
<li>
<p>判断是否存在key或filed &ndash; <strong>hexists</strong></p>
<ul>
<li>
<p><code>在程序中只写到key，不写field，判断是否存在该key</code></p>
</li>
<li>
<p>若存在，返回1</p>
</li>
<li>
<p>否则，返回0</p>
</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hexists <span style="color:#66d9ef">key</span> field 
</span></span></code></pre></div><ol start="4">
<li>查询一个key中所有数据 &ndash; <strong>hgetall</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hgetall <span style="color:#66d9ef">key</span>
</span></span></code></pre></div><p>​     <code>注意：</code>取到的结果会按hkey， hval的顺序排序，不会一一对应</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110123228.png" alt="image-20220302170017750"></p>
<ol start="5">
<li>查询出key中的所有hval &ndash; <strong>hvals</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hvals <span style="color:#66d9ef">key</span>
</span></span></code></pre></div><p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110123605.png" alt="image-20220302170347268"></p>
<ol start="6">
<li>对应5.查询出所有的hkey &ndash; <strong>hkeys</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hkeys <span style="color:#66d9ef">key</span>
</span></span></code></pre></div><ol start="7">
<li>删除数据 &ndash; <strong>hdel</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hdel <span style="color:#66d9ef">key</span> field [field...] <span style="color:#75715e">-- 删除key中对应的field数据，删除后返回对应条数
</span></span></span></code></pre></div><ol start="8">
<li>查看某key中元素长度 &ndash; <strong>hlen</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>hlen <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 查询key中元素长度
</span></span></span></code></pre></div><h2 id="list类型">list类型</h2>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110123567.png" alt="image-20220302171422259"></p>
<ol>
<li>添加数据 &ndash; <strong>lpush, rpush, linsert</strong>
<ul>
<li>list类型查询数据类似管道，<strong>l</strong>和<strong>r</strong>分别从左右两边依次存入数据</li>
<li><a href="">linsert</a>可以从已存在的数据前或后添加数据
<ul>
<li>before：在某元素之前添加</li>
<li>after：在某元素之后添加</li>
</ul>
</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>lpush <span style="color:#66d9ef">key</span> value [value...] <span style="color:#75715e">-- 从左边添加依次数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>rpush <span style="color:#66d9ef">key</span> value [value...] <span style="color:#75715e">-- 从右边依次添加数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>linsert <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">BEFORE</span><span style="color:#f92672">|</span><span style="color:#66d9ef">AFTER</span> pivot value
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">以上图为例：</span>
</span></span><span style="display:flex;"><span>linsert hobby <span style="color:#66d9ef">before</span> js <span style="color:#66d9ef">C</span><span style="color:#f92672">++</span> <span style="color:#75715e">-- 在js元素的前面添加一个C++
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>lrange hobby <span style="color:#ae81ff">0</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> <span style="color:#75715e">-- java, C++, js, dota2, lol
</span></span></span></code></pre></div><ol start="2">
<li>
<p>弹出数据 &ndash; <strong>lrange</strong></p>
<p>list类型查询数据类似管道，按照先进后查询到（从右边添加就在后面）</p>
<p><a href="">stop</a>为-1表示查询所有</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>lrange <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">start</span> stop <span style="color:#75715e">-- 表示从start的下标开始查询数据到stop，stop为-1时查询所有
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#960050;background-color:#1e0010">以上图为例：</span>
</span></span><span style="display:flex;"><span>lrange hobby <span style="color:#ae81ff">0</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> <span style="color:#75715e">-- java, js, dota2, lol
</span></span></span></code></pre></div><ol start="3">
<li>
<p>弹出数据 &ndash; <strong>lpop， rpop</strong></p>
<p>list类型类似于<strong>弹栈</strong>，string和hash类型使用<strong>get</strong>时对数据不会产生影响，</p>
<p>而list类型使用<strong>pop</strong>获取数据时，会弹出对应数据，并且数据将会从value中消失</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>lpop <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 从左边弹出第一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>rpop <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 从右边弹出第一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">以上图为例：</span>
</span></span><span style="display:flex;"><span>lpop hobby <span style="color:#75715e">-- java
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>rpop hobby <span style="color:#75715e">-- lol
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>lrange hobby <span style="color:#ae81ff">0</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> <span style="color:#75715e">-- &#34;js&#34;, &#34;dota2&#34;
</span></span></span></code></pre></div><ol start="4">
<li>查看key中<code>**有多少**</code>个元素 &ndash; <strong>llen</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>llen <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 查看有多少个元素（例如上图中，返回&#34;4&#34;）
</span></span></span></code></pre></div><ol start="5">
<li>**<code>修改</code>**对应下标的值 &ndash; <strong>lset</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>lset <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">index</span> value
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">以上图为例：</span>
</span></span><span style="display:flex;"><span>lset hobby <span style="color:#ae81ff">2</span> <span style="color:#66d9ef">c</span><span style="color:#f92672">#</span> <span style="color:#75715e">-- OK
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>lrange hobby <span style="color:#ae81ff">0</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> <span style="color:#75715e">-- &#34;java&#34;, &#34;js&#34;, &#34;c#&#34;, &#34;lol&#34;
</span></span></span></code></pre></div><ol start="6">
<li>根据下标索引<strong>查询对应数据</strong> &ndash; <strong>lindex</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>lindex <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">index</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">以上图为例：</span>
</span></span><span style="display:flex;"><span>lindex hobby <span style="color:#ae81ff">2</span> <span style="color:#75715e">-- &#34;dota2&#34;
</span></span></span></code></pre></div><h2 id="set类型无序不重复">set类型（无序不重复）</h2>
<ol>
<li>
<p>添加数据 &ndash; <strong>sadd</strong></p>
<p><a href="">sadd：</a></p>
<ul>
<li>当添加数据时没有该元素，则返回对应条数</li>
<li>否则，返回0</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>sadd <span style="color:#66d9ef">key</span> member [member ...] <span style="color:#75715e">-- 真是memer，没打错字
</span></span></span></code></pre></div><ol start="2">
<li>查看数据 &ndash; <strong>smembers</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>smembers <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 查询到对应key中所有的value（无序）
</span></span></span></code></pre></div><ol start="3">
<li>删除数据 &ndash; <strong>srem</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>srem <span style="color:#66d9ef">key</span> menber [member ...] <span style="color:#75715e">-- 删除key中的menber数据
</span></span></span></code></pre></div><ol start="4">
<li>
<p>随机弹出数据 &ndash; <strong>spop</strong></p>
<p>set类型的<a href="">pop</a>区别于list类型，这里只能随机弹出，但是可以指定数量</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>spop <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">count</span>] <span style="color:#75715e">-- count填数字
</span></span></span></code></pre></div><ol start="5">
<li>
<p><strong>交集(sinter)， 差集(sdiff)， 并集(sunion),  <code>+store</code></strong></p>
<ul>
<li>
<p><a href="">sdiff：</a></p>
<p>取差集，取key1中有的，但是key2中没有的</p>
</li>
<li>
<p><a href="">sinter：</a></p>
<p>取交集，取key1和key2中同时存在的</p>
</li>
<li>
<p><a href="">sunion:</a></p>
<p>取并集，取出key1和key2中所有的value</p>
</li>
<li>
<p><a href="">+store：</a></p>
<p>交集(sinter)， 差集(sdiff)， 并集(sunion)后加上store 变成</p>
<p><code>sinterstore, sdiffstore, sunionstore</code>表示生成一个新的集合用来存放交集，差集，并集运算后的结果</p>
</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>sdiff <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...] <span style="color:#75715e">-- 差集
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>sdiffstore destination <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...] 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>sinter <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...] <span style="color:#75715e">-- 交集
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>sinter store destination <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>sunion <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...] <span style="color:#75715e">-- 并集
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>sunionstore destination <span style="color:#66d9ef">key</span> [<span style="color:#66d9ef">key</span> ...]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">以并集为例：</span>
</span></span><span style="display:flex;"><span>smembers set1 <span style="color:#75715e">-- a, b, c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>smembers set2 <span style="color:#75715e">-- d, e, f
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>sunionstore set3 set1 set2 
</span></span><span style="display:flex;"><span>smembers sete3 <span style="color:#75715e">-- a, b, c, d, e, f
</span></span></span></code></pre></div><ol start="6">
<li>查看value个数 &ndash; <strong>scard</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>scard <span style="color:#66d9ef">key</span> <span style="color:#75715e">-- 查看key中有多少个value
</span></span></span></code></pre></div><ol start="7">
<li>删除value &ndash; <strong>srem</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>srem member [member ...] <span style="color:#75715e">-- 删除member的元素
</span></span></span></code></pre></div><ol start="8">
<li>从一个key把元素移动到另一个key &ndash; <strong>smove</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>smove <span style="color:#66d9ef">source</span> destination member <span style="color:#75715e">-- 把member从source移动到destination
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>smembers set1 <span style="color:#75715e">-- a, b, c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>smembers set2 <span style="color:#75715e">-- d, e, f
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>smove set1 set2 <span style="color:#66d9ef">c</span> <span style="color:#75715e">-- (intefer) 1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>smembers set1 <span style="color:#75715e">-- a, b
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>smembers set2 <span style="color:#75715e">-- c, d, e, f
</span></span></span></code></pre></div><ol start="9">
<li>判断集合中是否包含某元素 &ndash; <strong>sismember</strong>
<ul>
<li>若存在，返回1</li>
<li>否则，返回0</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>sismember <span style="color:#66d9ef">key</span> member <span style="color:#75715e">-- 判断key中是否包含member元素
</span></span></span></code></pre></div><h2 id="zset类型">zset类型</h2>
<ol>
<li>添加数据 &ndash; <strong>zadd</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>zadd <span style="color:#66d9ef">key</span> [NX<span style="color:#f92672">|</span>XX] [CH] [INCR] score member [score member ...]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>zadd myzset <span style="color:#ae81ff">2</span> b
</span></span><span style="display:flex;"><span>zadd myzset <span style="color:#ae81ff">1</span> a
</span></span><span style="display:flex;"><span>zrange myzset <span style="color:#ae81ff">0</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> <span style="color:#75715e">-- a, b(默认升序)
</span></span></span></code></pre></div><ol start="2">
<li>
<p>查询数据 &ndash; <strong>zrange(升序), zrevrange(降序)， zrank</strong></p>
<ul>
<li>
<p><code>withscores:</code></p>
<p>同时查看分数和名字时，在末尾加上withscores</p>
</li>
<li>
<p><code>zrank：</code></p>
<p>查询某个字段排序时的位置</p>
</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>zrange <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">start</span> stop [WITHSCORES] <span style="color:#75715e">-- 升序排列
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>zrevrange <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">start</span> stop [WITHSCORES] <span style="color:#75715e">-- 降序排列
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>zrank <span style="color:#66d9ef">key</span> member <span style="color:#75715e">-- 查询member在key中升序排序的位置
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>zrank myzset <span style="color:#66d9ef">c</span> <span style="color:#75715e">-- (integer) 1 即排在第二位（从0开始算，0， 1， 2）
</span></span></span></code></pre></div><ol start="3">
<li>
<p>数据运算 &ndash; <strong>zincrby</strong></p>
<ul>
<li>
<p><code>zincrby:</code></p>
<p>可加可减，加法（increment为正数），减法（increment为负数）</p>
</li>
</ul>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>zincrby <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">increment</span> member <span style="color:#75715e">-- 把key中的member加上increment
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">例：</span>
</span></span><span style="display:flex;"><span>zadd myzset <span style="color:#ae81ff">2</span> b
</span></span><span style="display:flex;"><span>zincrby myzset <span style="color:#ae81ff">90</span> b <span style="color:#75715e">-- 92
</span></span></span></code></pre></div><ol start="4">
<li>删除数据 &ndash; <strong>zrem</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>zrem <span style="color:#66d9ef">key</span> member [member ...] <span style="color:#75715e">-- 删除key中的member
</span></span></span></code></pre></div><ol start="5">
<li>查看一个区间内分数的个数 &ndash; <strong>zcount</strong></li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>zcount <span style="color:#66d9ef">key</span> <span style="color:#66d9ef">min</span> <span style="color:#66d9ef">max</span> <span style="color:#75715e">-- 查询分数在min到max中的个数
</span></span></span></code></pre></div><h2 id="redis管理操作">Redis管理操作</h2>
<ol>
<li>删除全部</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>flushall <span style="color:#75715e">-- 删除全部表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>flushdb <span style="color:#75715e">-- 删除当前数据库
</span></span></span></code></pre></div><ol start="2">
<li>查看配置信息</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>info
</span></span></code></pre></div>
    <h4><a href="https://czh-dev.gitee.io/czh-blog.gitee.io/">Back to Home</a></h4>
  </div>

  <div class="span3 bs-docs-sidebar" style="position:fixed;right: 40px;top: 50px;">
    <h1>catalogue</h1>
    <ul class="nav nav-list bs-docs-sidenav">
      <div class="toc-div">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#string类型">string类型</a></li>
    <li><a href="#hash类型">hash类型</a></li>
    <li><a href="#list类型">list类型</a></li>
    <li><a href="#set类型无序不重复">set类型（无序不重复）</a></li>
    <li><a href="#zset类型">zset类型</a></li>
    <li><a href="#redis管理操作">Redis管理操作</a></li>
  </ul>
</nav>
      </div>
    </ul>
  </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/gumshoejs@5.1.2/dist/gumshoe.min.js"></script>
<script>
  var spy = new Gumshoe('#TableOfContents a', {
    nested: true,
    nestedClass: 'active'
  });
</script>
<style>
   
  #TableOfContents li,
  #TableOfContents ul {
    list-style-type: none;
  }

  #TableOfContents ul {
    padding-left: 0px;
  }

  #TableOfContents li>a {
    display: block;
    padding: 4px 20px;
    font-size: 95%;
    color: #000000;
  }

  #TableOfContents li>a:hover,
  #TableOfContents li>a:focus {
    padding-left: 19px;
    color: #3A6bA5;
    text-decoration: none;
    background-color: transparent;
    border-left: 1px solid #3A6bA5;
  }

  #TableOfContents li.active>a,
  #TableOfContents li.active>a:hover,
  #TableOfContents li.active>a:focus {
    padding-left: 18px;
    font-weight: bold;
    color: #3A6bA5;
    background-color: transparent;
    border-left: 2px solid #3A6bA5;
  }

   
  #TableOfContents li>ul {
    padding-bottom: 10px;
  }

  #TableOfContents li li>a {
    padding-top: 1px;
    padding-bottom: 1px;
    padding-left: 30px;
    font-size: 14px;
    font-weight: normal;
  }

  #TableOfContents li li>a:hover,
  #TableOfContents li li>a:focus {
    padding-left: 29px;
  }

  #TableOfContents li li.active>a,
  #TableOfContents li li.active>a:hover,
  #TableOfContents li li.active>a:focus {
    padding-left: 28px;
    font-weight: 500;
  }

  #TableOfContents .nav-link.active+ul {
    display: block;
  }

  #TableOfContents li>ul {
    display: none;
  }

  #TableOfContents li.active>ul {
    display: inherit;
  }

  .toc-div {
    position: -webkit-sticky;
     
    position: sticky;
     
    top: 20px;
  }
</style>


        </div><footer class="container">
    <hr class="soften">
    <p>
    <a href="https://space.bilibili.com/1799809923">Love eating fried pork ribs</a> | 

&copy; 
<a href="http://jmf-portfolio.netlify.com" target="_blank">
    JM Fergeau
</a>
<span id="thisyear">2023</span>

    | My site


        | Built on <a href="//gohugo.io" target="_blank">Hugo</a>

</p>
    <p class="text-center">
        <a href="https://facebook.com">Facebook</a> 
        <a href="https://twitter.com">Twitter</a> 
        <a href="https://linkedin.com">Linkedin</a> 
        <a href="https://github.com">GitHub</a> 
        <a href="https://gitlab.com">GitLab</a>
    </p>
</footer>

</body><link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap-responsive.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/style.css">

<script src="/czh-blog.gitee.io/js/jquery.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-386.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-transition.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-alert.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-modal.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-dropdown.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-scrollspy.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tab.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tooltip.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-popover.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-button.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-collapse.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-carousel.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-typeahead.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-affix.js"></script>
<script>
    _386 = { 
        fastLoad: false ,
        onePass: false , 
        speedFactor: 1 
    };

    
    function ThisYear() {
        document.getElementById('thisyear').innerHTML = new Date().getFullYear();
    };
</script>
</html>
